//
//  CrazyChatAddExtensionBackView.m
//  webSocket
//
//  Created by 马军凯 on 2019/1/28.
//  Copyright © 2019年 ZH. All rights reserved.
//

#import "CrazyChatAddExtensionBackView.h"

@interface CrazyChatAddExtensionBackView () <UIScrollViewDelegate>
{
    UIPageControl *pageControl;
}

@property (nonatomic, weak) UIScrollView *scrollView; //scrollView
@property (nonatomic, assign) CGFloat distance; //间距
@property (nonatomic, assign) CGFloat width; //宽度

@end

@implementation CrazyChatAddExtensionBackView

+ (instancetype)sharedDefault {
    return [[self alloc] init];
}

- (instancetype)init {
    if (self = [super init]) {
        self.width = 60;
        self.distance = (CRAZY_SCREEN_WIDTH - self.width * 4) / 5;
        [self createScrollView];
    }
    return self;
}


#pragma mark 创建scrollView
- (void)createScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = CGRectMake(0, 0, CRAZY_SCREEN_WIDTH, 200);
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.bounces = NO;
    [self addSubview:scrollView];
    self.scrollView = scrollView;
    
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 200, 0, 40)];
    pageControl.numberOfPages = 2;
    pageControl.userInteractionEnabled = NO;
    pageControl.pageIndicatorTintColor = CRAZY_PAGECONTROL_NO_COLOR;
    pageControl.currentPageIndicatorTintColor = CRAZY_PAGECONTROL_YES_COLOR;
    pageControl.hidden = YES;
    [self addSubview:pageControl];
}










//赋值
- (void)setDataArray:(NSMutableArray *)dataArray {
    _dataArray = dataArray;
    if (dataArray.count <= 8) {
        pageControl.hidden = YES;
    } else {
        pageControl.hidden = NO;
    }
    //移除所有子视图
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //contentSize
    if (dataArray.count % 8 == 0) {
        self.scrollView.contentSize = CGSizeMake(CRAZY_SCREEN_WIDTH * (dataArray.count / 8), 0);
        pageControl.numberOfPages = (dataArray.count) / 8;
    } else {
        self.scrollView.contentSize = CGSizeMake(CRAZY_SCREEN_WIDTH * (dataArray.count / 8 + 1), 0);
        pageControl.numberOfPages = (dataArray.count) / 8 + 1;
    }
    //添加内容
    CGFloat x = self.distance;
    CGFloat y = 0;
    int num = 0;
    for (int i = 0; i < dataArray.count; i++) {
        CrazyChatAddExtensionModel *model = dataArray[i];
        UIView *backView = [self createViewWithFrame:CGRectMake(x, y, self.width, 100) image:model.image text:model.title];
        backView.tag = 20000 + i;
        backView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAddExtensionContent:)];
        [backView addGestureRecognizer:tap];
        [self.scrollView addSubview:backView];
        if ((i + 1) % 4 == 0) {
            if ((i + 1) % 8 == 0) {
                num = (i + 1) / 8;
                x = self.distance + CRAZY_SCREEN_WIDTH * num;
                y = 0;
            } else {
                x = self.distance + CRAZY_SCREEN_WIDTH * num;
                y = 100;
            }
        } else {
            x = x + self.width + self.distance;
        }
    }
}


#pragma mark 创建UIView的
- (UIView *)createViewWithFrame:(CGRect)frame image:(NSString *)image text:(NSString *)text {
    UIView *backView = [[UIView alloc] init];
    backView.frame = frame;
    //图片
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0, 10, self.width, self.width);
    imageView.image = [UIImage imageNamed:image];
    [backView addSubview:imageView];
    //名称
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.frame = CGRectMake(0, 80, self.width, 15);
    titleLabel.text = text;
    titleLabel.textColor = CRAZY_RGB(32, 46, 66);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:13];
    [backView addSubview:titleLabel];
    
    return backView;
}





#pragma mark UIScrollView
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControl.currentPage = scrollView.contentOffset.x / CRAZY_SCREEN_WIDTH;
}




#pragma mark 点击+号内内容
- (void)clickAddExtensionContent:(UIGestureRecognizer *)tap {
    CrazyChatAddExtensionModel *model = self.dataArray[tap.view.tag - 20000];
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectAddExtension:)]) {
        [self.delegate selectAddExtension:model];
    }
}

@end
